<script setup lang="ts">
import type { ElForm } from 'element-plus'
import { reactive, ref } from 'vue'

interface Iprops {
  searchConfig: {
    formItems: any[]
  }
}
const props = defineProps<Iprops>()
// 自定义事件
const emit = defineEmits(['resetClick', 'searchClick'])
const initialForm: any = {}
for (const item of props.searchConfig.formItems) {
  initialForm[item.prop] = item.initialValue ?? ''
}
const searchForm = reactive<any>(initialForm)
const useSearchFormRef = ref<InstanceType<typeof ElForm>>()
// 重置操作
const resetFormBtnClick = () => {
  useSearchFormRef.value?.resetFields()
  emit('resetClick')
}
// 搜索
//除去空格

function trim(str: string) {
  const reg = /\s+/g
  return str.replace(reg, '')
}
const searchFormBtnClick = () => {
  for (const item in searchForm) {
    searchForm[item] = trim(searchForm[item])
  }
  emit('searchClick', searchForm)
}
</script>
<template>
  <div class="search">
    <!-- 用户名 真实姓名 手机号码 状态 创建时间 重置 查询 -->
    <el-form label-width="100px" :model="searchForm" ref="useSearchFormRef">
      <el-row>
        <template v-for="item in searchConfig.formItems" :key="item.prop">
          <el-col :span="8">
            <el-form-item :label="item.label" :prop="item.prop">
              <!-- 动态组件 component is -->
              <template v-if="item.type === 'input'">
                <el-input
                  v-model="searchForm[item.prop]"
                  :placeholder="item.placeholder"
                />
              </template>
              <template v-if="item.type === 'select'">
                <el-select
                  v-model="searchForm[item.prop]"
                  :placeholder="item.placeholder"
                >
                  <template v-for="option in item.options" :key="option.label">
                    <el-option
                      :label="option.label"
                      :value="option.value"
                    ></el-option>
                  </template>
                </el-select>
              </template>
              <template v-if="item.type === 'date-picker'">
                <el-date-picker
                  v-model="searchForm[item.prop]"
                  type="daterange"
                  range-separator="-"
                  start-placeholder="开始时间"
                  end-placeholder="结束时间"
                />
              </template>
            </el-form-item>
          </el-col>
        </template>
      </el-row>
    </el-form>
    <!-- btn -->
    <div class="btnbox">
      <div class="btn-item">
        <el-button type="primary" plain @click="resetFormBtnClick">
          <el-icon><Refresh /></el-icon>
          重置
        </el-button>
      </div>
      <div class="btn-item">
        <el-button type="primary" @click="searchFormBtnClick">
          <el-icon><Search /></el-icon>
          搜索
        </el-button>
      </div>
    </div>
  </div>
</template>
<style scoped lang="less">
.search {
  padding: 20px;
  border-radius: 10px;
  background-color: #ffffff;
  .el-form-item {
    margin: 18px 0;
  }
  .el-select {
    width: 100%;
  }
  .btnbox {
    display: flex;
    justify-content: right;
    align-items: center;
    .btn-item {
      margin: 0 15px;
      .el-button {
        .el-icon {
          margin-right: 5px;
        }
      }
    }
  }
}
</style>
